<template>
  <div>
    <div class="nav">
      <van-nav-bar
        title="私聊"
        @click-left="onClickLeft"
        left-text="返回"
        left-arrow
      >
        <template #right>
          <van-tag plain type="primary">在线人数{{ pel }}{{ fid }}人</van-tag>
        </template>
      </van-nav-bar>
    </div>
    <div class="main">
      <div>
        <div class="left msg">
          <img src="../../assets/images/p.png" />
          <div class="nr">
            <div class="fliebox">
              <img src="../../assets/logo.png" alt="" />
              <div>
                <p>1090模型创建.docx</p>
                <p class="kbsize">12.5kb/已发送</p>
              </div>
            </div>
            <!-- <audio
              src="http://139.196.93.237:8050/static/img/1630487971641.mp3"
              controls="controls"
            ></audio> -->

            <div class="nt sharmgtime">123</div>
          </div>
        </div>

        <div class="center">
          <!-- <div class="inner" v-if="item.id == 3">{{ item.name }}</div> -->
        </div>
      </div>
      <div>
        <div class="left msg">
          <img src="../../assets/images/p.png" />
          <div class="nr">
            <div class="neir">
              <van-uploader
                accept=".pdf,.doc.,rar,.zip,.docx"
                :after-read="after"
                multiple
              />
              <a href="#" @click.prevent="xiazai">点击显示名</a>
            </div>
            <!-- <audio
              src="http://139.196.93.237:8050/static/img/1630487971641.mp3"
              controls="controls"
            ></audio> -->

            <div class="nt sharmgtime">123</div>
          </div>
        </div>

        <div class="center">
          <!-- <div class="inner" v-if="item.id == 3">{{ item.name }}</div> -->
        </div>
      </div>
      <div>
        <div class="left msg">
          <img src="../../assets/images/p.png" />
          <div class="nr">
            <div class="redBox">
              <p>恭喜发财，大吉大利</p>
            </div>

            <!-- <audio
              src="http://139.196.93.237:8050/static/img/1630487971641.mp3"
              controls="controls"
            ></audio> -->

            <div class="nt sharmgtime">123</div>
          </div>
        </div>

        <div class="center">
          <!-- <div class="inner" v-if="item.id == 3">{{ item.name }}</div> -->
        </div>
      </div>
      <div>
        <div class="right msg">
          <img src="../../assets/images/p.png" />
          <div class="nr">
            <div class="neir">
              <div @click="play">
                <img
                  src="../../assets/icon/iconVoice.png"
                  class="voice-img"
                  alt=""
                />
                <p class="timeout">23″</p>

                <audio id="musicfx" ref="audio">
                  <source
                    src="http://139.196.93.237:8050/static/img/1630487971641.mp3"
                    type="audio/mpeg"
                  />
                </audio>
              </div>
            </div>
            <!-- <audio
              src="http://139.196.93.237:8050/static/img/1630487971641.mp3"
              controls="controls"
            ></audio> -->

            <div class="nt sharmgtime">123</div>
          </div>
        </div>

        <div class="center">
          <!-- <div class="inner" v-if="item.id == 3">{{ item.name }}</div> -->
        </div>
      </div>
    </div>
    <div style="height: 5.5rem" class="fbox"></div>
    <div class="send">
      <div>
        <img
          src="../../assets/images/i.png"
          @click="showEmojin = true"
          class="emjonBtn"
          alt=""
        />
      </div>
      <textarea
        id="emojiInput"
        ref="texttemplate"
        v-model="emojitext"
        :auto-size="{ minRows: 6, maxRows: 15 }"
        placeholder="请输入内容"
        allow-clear
        class="chat-send"
      />
      <img @click="send" class="sendimg" src="../../assets/images/send.png" />
      <transition name="van-slide-up">
        <div class="Picker" v-if="showEmojin">
          <VEmojiPicker
            style="width: 100%; height: 422px"
            @select="selectEmoji"
          />
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import myfun from "../../utils/timeDate.js";
import { upload } from "../../utils/Fileupload";
import { mapState } from "vuex";

let Base64 = require("js-base64").Base64; //用于字符串编码 解码 用来存储表情

import io from "vue-socket.io";
// const socket = io("http://127.0.0.1:8082");

export default {
  sockets: {
    connect() {
      console.log("链接成功");
    },
    oneMsg(res) {
      console.log(res);
    },
    omMsg(data) {
      console.log(data);
    },
    joinOne(val) {
      console.log(val);
    },
  },
  data() {
    return {
      showEmojin: false,
      emojitext: "",
      pel: 0,
      uid: 0,
      fid: 0,
      chat1: [],
      welMsg: "",

      biaoji: 1,
      chat: [
        {
          name: "小明",
          neir: "sdsagfegfdgsf",
          img: "../../assets/images/j.png",
          time: new Date(),
          id: 11,
        },
        {
          name: "欢迎小明",

          id: 3,
        },
        {
          name: "小明",
          neir: "啊实打实",
          img: "../../assets/images/j.png",
          time: new Date(),
          id: 11,
        },
        {
          name: "小是",
          neir: "大萨达",
          img: "../../assets/images/p.png",
          time: new Date(),
          id: 17,
        },
      ],
      tochat: [],
      roomList: [],
    };
  },
  mounted() {
    this.socketApi.initWebSocket();
  },
  created() {
    this.jieshou();
    this.Getonemsg();
    this.getMsg();
  },
  destroyed() {},
  methods: {
    play() {
      this.$refs.audio.play();
    },
    selectEmoji(emoji) {
      var elInput = document.getElementById("emojiInput"); //获取输入框元素
      // console.log(elInput);
      var start = elInput.selectionStart; // 记录光标开始的位置
      var end = elInput.selectionEnd; // 记录选中的字符 最后的字符的位置
      if (start === undefined || end === undefined) return;
      var txt = elInput.value;
      // 将表情添加到选中的光标位置
      var result = txt.substring(0, start) + emoji.data + txt.substring(end);
      elInput.value = result; // 赋值给input的value
      // 重置光标位置
      elInput.focus();
      elInput.selectionStart = start + emoji.data.length;
      elInput.selectionEnd = start + emoji.data.length;
      this.emojitext = result; // 赋值(注意这里一定要赋值给表情输入框绑定的那个值)
    },
    getview(to, from) {
      this.fid = this.$route.params.fid;
    },
    getMsg() {
      this.$socket.emit("joinOne", this.emailLogin[0].userID);
    },
    onClickLeft() {
      this.$router.push("/Message");
    },
    changetime(t) {
      return myfun.dateTime(t);
    },
    send() {
      let data = {
        id: this.emailLogin[0].userID,
        receive_id: 17,
        user: this.emailLogin[0].Nickname,
        userImage: this.emailLogin[0].userImg,
        dateTime: new Date(),
        msg: Base64.encode("你sssdaass好ss"),
        msgType: 2, //代表私聊消息
        textType: "文字",
      };
      this.socketApi.handleSendMsg(data);
      this.tochat.push(data);
    },
    jieshou() {
      const r = this.socketApi.handleWsMessage();
      console.log(r);
    },
    //接收一对一方法
    Getonemsg() {
      this.sockets.subscribe("omMsg", (data) => {
        console.log(data);
        this.tochat.push(data);
      });
    },
    after(file) {
      const a = upload.uploadChange(file);
    },
    xiazai() {},
  },
  watch: {
    $route: {
      handler(newVal, oldVal) {
        this.getview(newVal, oldVal);
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    ...mapState(["emailLogin"]),
  },
};
</script>
<style lang="less" scoped>
.nav {
  position: fixed;
  left: 0;
  width: 100%;
  z-index: 1200;
}
.main {
  padding: 40px 15px 10px;
}
.main,
.modfiy-mian {
  .msg {
    display: flex;

    align-items: flex-end;
    padding: 20px 0;
    img {
      flex: none;
      width: 40px;
      height: 40px;
      border-radius: 18px;
      margin: 0 8px;
    }
    .nr {
      .neir {
        padding: 10px 26px;
        min-height: 20px;
        font-size: 14px;
        color: #191d23;
        line-height: 20px;
        max-width: 200px;
      }
      .nt {
        padding-top: 10px;
        position: absolute;
        font-size: 12px;
        color: rgba(25, 29, 35, 0.5);
        line-height: 10px;
      }
    }
  }

  .left {
    flex-direction: row;
    .neir {
      background: url(../../assets/bahj.png);
      background-size: 100% 100%;
      background: rgba(244, 244, 244, 1);
      border-radius: 24px 24px 24px 0px;
      .voice-img {
        width: 20px !important;
        height: 20px !important;
        float: left !important;
        margin-left: -15px !important;
      }
      .timeout {
        float: right;
      }
    }
  }
  .right {
    flex-direction: row-reverse;
    .neir {
      background: rgba(255, 234, 222, 1);
      border-radius: 24px 24px 0px 24px;
      .voice-img {
        width: 20px !important;
        height: 20px !important;
        float: right !important;
        margin-right: -15px !important;
        transform: rotate(180deg);
      }
      .timeout {
        float: left;
      }
    }
    .nt {
      right: 78px;
    }
  }
  .center {
    text-align: center;
    padding: 20px 0 2px;
    .inner {
      font-size: 14px;
      display: inline-block;
      color: rgba(25, 29, 35, 0.5);
      line-height: 15px;
    }
  }
}
.send {
  position: fixed;
  z-index: 1002;
  bottom: 0;
  width: 100%;
  padding-bottom: 10px;
  min-height: 40px;
  background: rgba(255, 255, 255, 0.96);
  display: flex;
  flex-direction: column;
  .chat-send {
    margin: 4px 40px;
    flex: 1;

    min-height: 21px;
    background: #f2f2f2;
    border-radius: 11px;
    font-size: 15px;
    color: #191d23;
    line-height: 12px;
    padding: 8px;
  }
  .sendimg {
    width: 25px;
    height: 25px;
    position: absolute;
    right: 45px;
    top: 13px;
  }
}
.modify {
  position: fixed;
  overflow: hidden;
  left: 0;
  bottom: -90%;
  width: 100%;
  height: 90%;
  z-index: 1000;
  background: #fff;
  border-radius: 48px 48px 0px 0px;
}
.u-name {
  position: absolute;
  top: 0;
  background-color: #fff;
  height: 80px;
  text-align: center;
  width: 100%;
  line-height: 80px;
}
.modfiy-mian {
  padding: 88px 28px 120px;
  box-sizing: border-box;
  height: 100%;
}
.bg {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  background-color: rgba(0, 0, 0, 0.4);
}
.Picker {
  height: 260px;
}
.emjonBtn {
  width: 25px;
  height: 25px;
  position: absolute;
  top: 13px;
  margin-left: 6px;
}
.textimg {
  width: 142px;
  height: 160px;
  .imgt {
    width: 142px !important;
    height: 160px !important;
    border-radius: 10px !important;
  }
}
.Share_item {
  border: 1px solid #e6e6e6;
  width: 250px;
  margin-top: -40px;
  position: absolute;
}
.sharmgtime {
  margin-top: 70px;
}
.per_crad {
  width: 200px;
  height: 80px;
  background: url(/img/Pecards.70f5c188.png) no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
  border-radius: 10px;
  .per_img {
    img {
      width: 40px;
      height: 40px;
      border: 2px solid white;
      border-radius: 4px !important;
    }
  }
  p {
    color: white;
    font-size: 17px;
  }
}
.audiocss {
  width: 140px;
  height: 60px;
  background: url(../../assets/icon/shengbo.png) no-repeat;
  background-size: cover;
}
.wifi-symbol {
  width: 50px;
  height: 50px;
  box-sizing: border-box;
  overflow: hidden;
  transform: rotate(135deg);
}
.wifi-circle {
  border: 5px solid #999999;
  border-radius: 50%;
  position: absolute;
}

.first {
  width: 5px;
  height: 5px;
  background: #cccccc;
  top: 45px;
  left: 45px;
}

.bg {
  background: url(../../assets/icon/iconVoice.png) right 0 no-repeat;
  width: 24px;
  height: 24px;
  background-size: 400%;
}

.voicePlay {
  animation-name: voicePlay;
  animation-duration: 1s;
  animation-direction: normal;
  animation-iteration-count: infinite;
  animation-timing-function: steps(3);
}

@keyframes voicePlay {
  0% {
    background-position: 0;
  }
  100% {
    background-position: 100%;
  }
}
.fliebox {
  width: 200px;
  height: 80px;
  display: flex;
  align-items: center;
  border-radius: 10px;
  border: 1px solid;
  img {
    border: 1px solid #c9c9c9 !important;
    width: 40px !important;
    height: 40px !important;
    border-radius: 0 !important;
  }
}
.kbsize {
  font-size: 13px;
  color: #b1b1b1;
}
.redBox {
  background: url(https://sss-1306551685.cos.ap-guangzhou.myqcloud.com/MobileShop/Redenvelope.png)
    no-repeat;
  background-size: 100% 100%;
  width: 230px;
  height: 100px;
  position: relative;
  top: 52px;
  left: -6px;
  p {
    color: white;
    font-size: 16px;
    left: 58px;
    position: absolute;
    top: 25px;
    font-weight: 600;
  }
}
</style>